﻿using System;
using System.Web.Mvc;
using MyAlbum.Model;
using MyAlbum.Data;
using MyAlbum.Logging;
using MyAlbum.Services;

namespace PersonalAlbum.Controllers
{
    public class LogEntriesAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            try
            {

                if (ObjectFactory.GetInstance<IConfiguration>().LogEntrances)
                {
                    string sessionId = filterContext.HttpContext.Session != null ? filterContext.HttpContext.Session.SessionID : null;
                    ObjectFactory.GetInstance<IDataService<Entrance>>().AddItem(new Entrance()
                    {
                        IP = filterContext.HttpContext.Request.UserHostAddress,
                        Page = filterContext.HttpContext.Request.Url.OriginalString,
                        SessionId = sessionId,
                        Timestamp = DateTime.UtcNow
                    },null);
                }
            }
            catch (Exception e1)
            {
                try
                {
                    WebLogger.LogError(e1, "log entrance");
                }
                catch { }
            }
            base.OnResultExecuted(filterContext);
        }
      
    }
}
